package jgame;

import java.util.ArrayList;

import jgame.model.BadHuman;
import jgame.model.Human;
import jgame.model.Position;
import jgame.model.Unit;
import jgame.model.movingTypes.Running;
import jgame.Log;

public class GameManager {
	ArrayList<Unit> units = new ArrayList<Unit>();
	int lastUpdate;

	
	public GameManager() {
		Log.add("GameManager up!");
		GuiManager.getInstance().setGameManager(this);
	}
	
	public void run() {
		Log.add("GameManager entry endless run-method");
		lastUpdate = (int)System.currentTimeMillis();
		
		// now simulate some things in the game as it would in a round
		Human cop = new Human();
		units.add(cop);
		cop.setPosition(new Position(10,10));
		cop.setCurrentMovingType(new Running());
		
		BadHuman bastard = new BadHuman();
		units.add(bastard);
		bastard.setPosition(new Position(100,100));
		
		cop.moveTo(bastard);
		
		
		while(true) {
			this.update();
			this.draw();
		}
	}

	public void update() {
		int now = (int)System.currentTimeMillis();
		int ticks = (int)((now - lastUpdate) / 10);

		lastUpdate = now;

		for(int i=0; i<ticks; i++) {
			for (Unit unit : units) {
				unit.moveTick();
			}
			for (Unit unit : units) {
				for (Unit discoverUnit : units) {
					if(unit != discoverUnit) {
						if(unit.distanceTo(discoverUnit) <= unit.getSight().getRadius()) {
							unit.discoverTick(discoverUnit);
						}
					}
				}
			}
		}
	
	}
	
	public void draw() {
		//Log.add("Now rendering:");
		
		GuiManager.getInstance().drawPanel.repaint();
		/*for(Unit unit : units) {
			Log.add("- Unit: " + unit.toString());
		}*/
		// just a hack to simulate the latency of this.draw() :
		 try { Thread.sleep((long) (100)); } catch (InterruptedException e) {}

	//	Log.add("Done rendering!");
	}
}
